package com.can.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.can.Mapper.GoodBadMapper;
import com.can.Mapper.PostMapper;
import com.can.Service.PostService;
import com.can.domain.Dto.CreatePostDto;
import com.can.domain.po.Goodbad;
import com.can.domain.po.post;
import com.can.domain.vo.postcommonvo;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class postImpl implements PostService {
    @Autowired  PostMapper postMapper;
    @Autowired  GoodBadMapper goodBadMapper;
    @Autowired  private RedisTemplate<String, String> redisTemplate;
    @Autowired

    private final RabbitTemplate rabbitTemplate;
    @Override
    public postcommonvo getPostsBySectionId(int sectionid) {
        try {
            QueryWrapper<post> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("section_id", sectionid); // 等价于 SQL：WHERE section = 'your_section_value'
            List<post> res = postMapper.selectList(queryWrapper);
            return new postcommonvo(200,"查询成功",res);
        }catch (Exception e){
            e.printStackTrace();
            return new postcommonvo(400,"查询失败",null);
        }


    }

    @Override
    public postcommonvo deletePosById(int postid) {
        try {
            postMapper.deleteById(postid);
                redisTemplate.delete("post:" + postid + ":good");
                redisTemplate.delete("post:" + postid + ":bad");
            return new postcommonvo(200,"删除成功",null);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return new postcommonvo(500,"删除失败",null);
        }
    }


//    发帖
    @Override
    public postcommonvo createPost(int userId, CreatePostDto releasedto) {
        try {
//            更新数据到redis和帖子表和点赞表
            post newpost = new post().dtoToEntity(userId,releasedto);
            postMapper.insert(newpost);
            System.out.println("新的帖子参数："+newpost);
            redisTemplate.opsForValue().set("post:"+newpost.getId()+":good","0");
            redisTemplate.opsForValue().set("post:"+newpost.getId()+":bad","0");
            goodBadMapper.insert(new Goodbad(newpost.getId(),0,0));
            rabbitTemplate.convertAndSend("exp.fanout", "", userId);
            return new postcommonvo(200,"发帖成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return new postcommonvo(400,"查询失败",null);
        }

    }


//    查询帖子基本信息
    @Override
    public postcommonvo getPostByPostId(int postId) {
        try {
//            QueryWrapper<post> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("Id", postId);
//            post res = postMapper.selectOne(queryWrapper);
            post res = postMapper.getPostByPostId(postId);
            return new postcommonvo(200,"查询成功",res);
        }catch (Exception e){
            e.printStackTrace();
            return new postcommonvo(400,"查询失败",null);
        }
    }
}
